package com.naiterui.ehp.bs.emr.controller;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.context.ResourceHolder;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.security.SettingConfig;
import com.naiterui.ehp.bs.emr.service.IPatientDiseaseService;
import com.naiterui.ehp.bs.emr.utils.enums.DiseaseCycle;
import com.naiterui.ehp.bs.emr.vo.UploadFileVO;
import com.naiterui.ehp.bs.emr.vo.patient.PatientDiseaseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Description 患者病情
 * @Author gongbaoqiang
 * @Date 2019/11/28 12:08 PM
 */
@Slf4j
@Validated
@RestController
@RequestMapping("emr/patient/disease")
@Api(tags = SwaggerTag.PATIENT + SwaggerTag.MEDICAL_RECORD)
public class EmrPatientDiseaseController {

    @Autowired
    private IPatientDiseaseService diseaseService;

    @ApiOperation(value = "获取病情周期列表", notes = "病情描述——患病多久了列表")
    @PostMapping("cycle/list")
    public ResponseVO<List<DiseaseCycle>> cycleList() throws BusinessException {
        return PublicService.returnResponseVO(diseaseService.cycleList());
    }

    @ApiOperation(value = "保存病情", notes = "保存病情信息")
    @PostMapping("add")
    public ResponseVO<String> addPatientDisease(Long patientId, @Valid @RequestBody PatientDiseaseVO patientDisease)
        throws BusinessException {
        if (null == patientId) {
            patientId = (Long)ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        log.info("患者提交病情描述 请求参数：patientDisease = {}", patientDisease);
        patientDisease.setPatientId(patientId);
        String patientDiseaseId = diseaseService.add(patientDisease);
        return PublicService.returnResponseVO(patientDiseaseId);
    }

    /**
     * 上传病情图片
     * 
     * @param files
     * @return
     */
    @PostMapping("img/upload")
    public ResponseVO<List<String>> uploadDiseaseImg(MultipartFile[] files) {
        log.info("上传病情图片，参数 {}", files);
        List<String> path = diseaseService.uploadDiseaseImg(files);
        return PublicService.returnResponseVO(path);
    }

    @ApiOperation(value = "获取患者病情", notes = "获取患者病情")
    @PostMapping("detail")
    public ResponseVO<PatientDiseaseVO> detail(String diseaseId) throws BusinessException {
        log.info("获取病情描述 请求参数：id = {}", diseaseId);
        PatientDiseaseVO patientDiseaseId = diseaseService.detail(diseaseId);
        return PublicService.returnResponseVO(patientDiseaseId);
    }

    @ApiOperation(value = "上传Pdf", notes = "上传线下病例/处方pdf")
    @PostMapping("pdf/upload")
    public List<UploadFileVO> uploadPdf(@NotNull(message = "上传文件不能为null") MultipartFile[] files)
        throws BusinessException {

        return diseaseService.uploadPatientFile(files);
    }

    @ApiOperation(value = "病情列表", notes = "病情列表")
    @PostMapping("list")
    public PageVO<PatientDiseaseVO> list(Long patientId, PageParamVO pageParamVO) throws BusinessException {
        if (null == patientId) {
            patientId = (Long)ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        log.info("获取病情列表 请求参数：patientId = {}", patientId);

        return diseaseService.list(patientId, pageParamVO);
    }
}
